//
//  MXHomeDetialController.m
//  CatchBear
//
//  Created by 王启颖 on 2018/11/13.
//  Copyright © 2018年 王启颖. All rights reserved.
//

#import "MXHomeDetialController.h"
#import "MXHomeDetialView.h"
#import "MXReportController.h"
#import "MXAuthController.h"


@interface MXHomeDetialController ()
@property(strong,nonatomic)MXHomeDetialView *tableview;
@property (nonatomic,assign) BOOL isBlack;
@end

@implementation MXHomeDetialController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self SetNavigationClear];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self setNavigationGradientBackImage];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isBlack = NO;
    
    [self tableview];
    
    [self barButtonItemName:@"" withImageName:@"transparentMore" targer:@"complaintClick" isRight:YES];
    
    [self barButtonItemName:@"" withImageName:@"transparentBack" targer:@"BackClick" isRight:NO];
    
}

- (void)BackClick{
     self.homeDetialBlock(self.model);
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)complaintClick {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController pushViewController:[[MXReportController alloc] init] animated:true];
    }]];
    
    NSString *alertTitle = self.isBlack ? @"在黑名单中" : @"加入黑名单";
    [alertController addAction:[UIAlertAction actionWithTitle:alertTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[MXAccount sharedMXAccount] LoadTokenFromSandbox];
        NSString *token = [MXAccount sharedMXAccount].token;
        if (![token length]) {
            [SVProgressHUD showImage:NULL_IMAGE status:@"登录后才能进行拉黑操作！"];
            MXAuthController *authVC = [[MXAuthController alloc]init];
            [self.navigationController pushViewController:authVC animated:true];
            return;
        }
        
        if (!self.isBlack) {
            [self addBlackUserRequest];
        }
        
    }]];
    
    [self presentViewController:alertController animated:YES completion:nil];
 
}

#pragma mark - 网络请求 -
-(void)addBlackUserRequest{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"user_id"] = [MXUserMessageTool message].user_id;
    dic[@"black_user_id"] = self.model.user.user_id;
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:addBlackUser params:dic isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];
        
        if (code == 0) {
            [SVProgressHUD showImage:NULL_IMAGE status:@"加入黑名单成功"];
            self.isBlack = YES;
            
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"参数错误"];
        }
        
    } fail:^(NSError *error) {
        [SVProgressHUD dismiss];
        NSLog(@"%@",error);
    }];
}

#pragma mark - 懒加载 -
-(MXHomeDetialView *)tableview{
    if (!_tableview) {
        
        MXHomeDetialView *view = [[MXHomeDetialView alloc]initWithFrame:CGRectMake(0,-KSafeAreaTopHeight, kScreenWidth, kScreenHeight-KSafeAreaBottonHeight)];
        __weak typeof(self) weakself = self;
        view.homeDetialBlock = ^(MXHomePageModel *model) {
            weakself.model = model;
        };
        _tableview = view;
        _tableview.model = self.model;
        _tableview.navigationController = self.navigationController;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}


@end
